package com.engine.jucailinkq.attendance.workflow.action;

import com.engine.jucailinkq.common.util.CommonUtil;
import com.engine.jucailinkq.common.util.DateUtil;
import com.engine.jucailinkq.common.util.DbTools;
import lombok.extern.slf4j.Slf4j;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 调动分组流程提交action
 * @author sy
 */
@Slf4j
public class AdjustPersonGroupCheckAction implements Action {


    @Override
    public String execute(RequestInfo requestInfo) {
        log.info("**********AdjustPersonGroupCheckAction start**********");
        String requestid = requestInfo.getRequestid();
        // 流程表单主表数据
        HashMap<String,String> mainTableData = CommonUtil.getMainTableInfo(requestInfo);
        log.info("mainTableData : [{}]",mainTableData);
        //调动人员
        String ddry = Util.null2String(mainTableData.get("ddry"));
        //调入日期
        String adjustDate = Util.null2String(mainTableData.get("drrq"));
        //调入分组
        String adjustToGroup = Util.null2String(mainTableData.get("drfz"));
        //调出分组
        String adjustFromGroup = Util.null2String(mainTableData.get("dcfz"));
        //调回日期
        String adjustBackDate = Util.null2String(mainTableData.get("dhrq"));
        //调动说明
        String ddsm = Util.null2String(mainTableData.get("ddsm"));

        try {
            String sql = "";
            if (!"".equals(adjustFromGroup)) {
                sql = "select id, bdate, edate from uf_ryqz_dt1 where mainid = " + adjustFromGroup + " and empid = " + ddry + " and delete_type = 0";
                List<Map<String, Object>> data = DbTools.getSqlToList(sql);
                List<Map<String, Object>> targetData = data.stream().
                        filter(f -> Util.null2String(f.get("bdate")).compareTo(adjustDate) < 0
                                && ("".equals(Util.null2String(f.get("edate"))) || Util.null2String(f.get("edate")).compareTo(adjustDate) >= 0))
                        .collect(Collectors.toList());
                if (targetData.size() > 2) {
                    log.error("调出分组中目标人员有效的数据不唯一！");
                    requestInfo.getRequestManager().setMessageid("11111" + requestInfo.getRequestid() + "22222");
                    requestInfo.getRequestManager().setMessagecontent("调出分组中目标人员有效的数据不唯一！");
                    return Action.FAILURE_AND_CONTINUE;
                } else if (targetData.size() == 0) {
                    log.error("调出分组中目标数据不存在！");
                    requestInfo.getRequestManager().setMessageid("11111" + requestInfo.getRequestid() + "22222");
                    requestInfo.getRequestManager().setMessagecontent("调出分组中目标数据不存在！");
                    return Action.FAILURE_AND_CONTINUE;
                }
                //调回日期不为空时，需要晚于调入日期
                if (!"".equals(adjustBackDate)) {
                    if(adjustBackDate.compareTo(adjustDate) <= 0) {
                        log.error("调回日期不为空时，需要晚于调入日期！");
                        requestInfo.getRequestManager().setMessageid("11111" + requestInfo.getRequestid() + "22222");
                        requestInfo.getRequestManager().setMessagecontent("调回日期不为空时，需要晚于调入日期！");
                        return Action.FAILURE_AND_CONTINUE;
                    }
                    //判断调入分组中新增的调回数据，会不会出现交集有效时段
                    String targetDataId = targetData.get(0).get("id").toString();
                    String endDate = "";
                    //去除被调出的目标数据
                    data = data.stream().filter(f -> !f.get("id").toString().equals(targetDataId)).collect(Collectors.toList());
                    //遍历判断剩余数据
                    for(Map<String, Object> dataItem : data) {
                        endDate = Util.null2String(dataItem.get("edate"));
                        //判断是否不存在交集
                        boolean noOverlapSign = !"".equals(endDate) && adjustDate.compareTo(endDate) > 0;
                        if (!noOverlapSign) {
                            log.error("调出分组新增调回明细数据与未来生效的原有明细数据存在交集，不可提交！");
                            requestInfo.getRequestManager().setMessageid("11111" + requestInfo.getRequestid() + "22222");
                            requestInfo.getRequestManager().setMessagecontent("调出分组新增调回明细数据与未来生效的原有明细数据存在交集，不可提交！");
                            return Action.FAILURE_AND_CONTINUE;
                        }
                    }
                }
            } else {
                //调出分组为空时，不允许设置调回日期
                if (!"".equals(adjustBackDate)) {
                    log.error("调出分组为空时，不允许设置调回日期！");
                    requestInfo.getRequestManager().setMessageid("11111" + requestInfo.getRequestid() + "22222");
                    requestInfo.getRequestManager().setMessagecontent("调出分组为空时，不允许设置调回日期！");
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
            if (!"".equals(adjustToGroup)) {
                sql = "select id, bdate, edate from uf_ryqz_dt1 where mainid = " + adjustToGroup + " and empid = " + ddry + " and delete_type = 0";
                List<Map<String, Object>> data = DbTools.getSqlToList(sql);
                //收集一下，调入分组中目标人员的有效明细数据是否和新增数据有交集
                List<Map<String, Object>> targetData = data.stream().
                        filter(f -> Util.null2String(f.get("bdate")).compareTo(adjustDate) <= 0
                                && ("".equals(Util.null2String(f.get("edate"))) || Util.null2String(f.get("edate")).compareTo(adjustDate) >= 0))
                        .collect(Collectors.toList());
                if (targetData.size() > 0) {
                    log.error("调入分组中已存在目标人员在调入日期的有效数据，不允许调入！");
                    requestInfo.getRequestManager().setMessageid("11111" + requestInfo.getRequestid() + "22222");
                    requestInfo.getRequestManager().setMessagecontent("调入分组中已存在目标人员在调入日期的有效数据，不允许调入！");
                    return Action.FAILURE_AND_CONTINUE;
                }
                //判定插入新数据后，是否有还未生效的数据和插入数据存在交集
                String startDate = "";
                String endDate = "";
                for(Map<String, Object> dataItem : data) {
                    startDate = Util.null2String(dataItem.get("bdate"));
                    endDate = Util.null2String(dataItem.get("edate"));
                    //判断是否不存在交集
                    boolean noOverlapSign = ("".equals(endDate) && !"".equals(adjustBackDate) && adjustBackDate.compareTo(startDate) < 0)
                            || (!"".equals(endDate) && !"".equals(adjustBackDate) && adjustBackDate.compareTo(startDate) < 0)
                            || (!"".equals(endDate) && !"".equals(adjustBackDate) && adjustDate.compareTo(endDate) > 0)
                            || (!"".equals(endDate) && "".equals(adjustBackDate) && adjustDate.compareTo(endDate) > 0);
                    if (!noOverlapSign) {
                        log.error("调入分组新增明细数据与未来生效的原有明细数据存在交集，不可提交！");
                        requestInfo.getRequestManager().setMessageid("11111" + requestInfo.getRequestid() + "22222");
                        requestInfo.getRequestManager().setMessagecontent("调入分组新增明细数据与未来生效的原有明细数据存在交集，不可提交！");
                        return Action.FAILURE_AND_CONTINUE;
                    }
                }

            } else {
                log.error("调入分组不可为空！");
                requestInfo.getRequestManager().setMessageid("11111" + requestInfo.getRequestid() + "22222");
                requestInfo.getRequestManager().setMessagecontent("调入分组不可为空！");
                return Action.FAILURE_AND_CONTINUE;
            }

        }catch (Exception e){
            log.error("AdjustPersonGroupCheckAction error : [{}]",e);
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;
    }
}
